﻿// Author:					HungNM
// Created:					22-08-2012
// Last Modified:			22-08-2012
//

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;


namespace eProject.Web.UI
{

    public partial class ChangePassPage : System.Web.UI.Page
    {
        private string OldPassword = string.Empty;
        private string NewPassword = string.Empty;
        private Users user;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadParams();
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            if (IsPostBack) return;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;
            try
            {
                user = SiteUser.getCurrentUser();
                if (user == null)
                {
                    SessionClass.AuthRequestPage = Request.Path;
                    Response.Redirect("~/Login.aspx");
                }
                OldPassword = txtOldPass.Text.Trim();
                string OldPasswordEncrypt = EncryptHelper.Encrypt(OldPassword, user.ChallengeCode, true);

                if (OldPasswordEncrypt.Equals(user.Password))
                {
                    NewPassword = txtNewPass.Text.Trim();
                    user.ChallengeCode = EncryptHelper.RandomString(10, false);
                    user.Password = EncryptHelper.Encrypt(NewPassword, user.ChallengeCode, true);
                    if (user.Save())
                    {
                        UserHelper.Instance.Remove();
                        UserHelper.Instance.AddUser(user.Id);
                        Result.Style["color"] = "Green";
                        ResultError.Text = "Change password success.";
                        Result.Visible = true;
                    }
                }
                else
                {
                    ResultError.Text = "Old password not match.";
                    Result.Visible = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                user = null;
            }
        }

        private void PopulateLabels()
        {
            if (IsPostBack) return;
            Title = "Change Pass Page - eProject";
            ltHeader.Text = "Change Password";
            lblOldPass.Text = "Current Password";
            lblNewPass.Text = "New Password";
            lblConfirm.Text = "Confirm Password";
        }

        private void LoadSettings()
        {
            if (IsPostBack) return;
            user = SiteUser.getCurrentUser();
            if (user == null)
            {
                SessionClass.AuthRequestPage = Request.Path;
                Response.Redirect("~/Login.aspx");
            }
        }

        private void LoadParams()
        {

        }

        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.btnSave.Click += new EventHandler(btnSave_Click);
        }

        #endregion
    }
}